<?php
namespace Components\Account\User;

class Service {
	private $models = [];
	public function getModelsById($id = null) {
		if(is_null($id)) {
			return $this -> models;
		}

		if(!is_numeric($id) || $id <= 0) {
			return false;
		}

		if(isset($this -> models[$id]) && !empty($this -> models[$id]) && is_object($this -> models[$id])) {
			return $this -> models[$id];
		}

		$model = $this -> findFirstById($id);
		if($model['code'] != 0) {
			return false;
		}

		return $this -> models[$id] = $model['user'];
	}

	public function findFirst($parameters = []) {
		$model = Model::findFirst($parameters);
		if(!$model) {
			return ['code' => -1, 'message' => '数据不存在或已删除'];
		}

		return ['code' => 0, 'user'=>$model];
	}

	public function findFirstById($id) {
		if($id <= 0) {
			return ['code' => -1, 'message' => '数据不存在或已删除'];
		}

		return $this -> findFirst(['user_id = ' . $id]);
	}

	public function findFirstByAccount($account) {
		if(empty($account)) {
			return ['code' => -1, 'message' => '数据不存在或已删除'];
		}

		return $this -> findFirst(['account = "' . $account . '"']);
	}

	
	public function find($parameters = []) {
		$model = Model::find($parameters);
		if(!$model -> valid()) {
			return ['code' => -1, 'message' => '数据不存在或已删除'];
		}

		return ['code' => 0, 'user'=>$model];
	}
	

	public function loginToken($data) {
		$model = Adapter::getModelInstance() -> findFirst('account = "' . $data['account'] . '"');
		if(!$model || !$model -> getDI() -> get('security') -> checkHash($data['password'], $model -> password)) {
			return ['code' => -1, 'message' => '账号或密码错误'];
		}

		switch($model -> status) {
			case 1:
			case '1':
				return ['code' => -1, 'message' => '账号未激活'];
				break;
			case 2:
			case '2':
				return ['code' => -1, 'message' => '账号已冻结'];
				break;
			case 3:
			case '3':
				//return ['code' => -1, 'message' => '账号已封号'];
				break;
		}

		return ['code' => 0, 'token'=>(new JWT) -> setToken(['account'=>['user_id'=>$model->user_id]]), 'account'=>$model];
	}

	public function checkLogin($token) {
		if(!isset($token) || is_null($token) || empty($token)) {
			return ['code' => -1, 'message' => 'token 未设置!'];
		}

		$checkTokenJWT = (new JWT()) -> checkToken($token);
		if($checkTokenJWT['code'] != 0) {
			return $checkTokenJWT;
		}

		if(!$this -> getModelsById($checkTokenJWT['payload']['account']['user_id'])) {
			return ['code' => -1, 'message'=>'数据不存在或已删除'];
		}

		switch($this -> models[$checkTokenJWT['payload']['account']['user_id']] -> status) {
			case 2:
			case '2':
				return ['code' => -1, 'message'=>'账号已冻结'];
			case 3:
			case '3':
				return ['code' => -1, 'message' => '账号已封号'];
				break;
		}

		return ['code' => 0, 'user' => $this -> models[$checkTokenJWT['payload']['account']['user_id']]];
	}

	public function create($data) {
		$model = new Model($data);
		if(!$model -> create()) {
			$messages = $model -> getMessages();
			return ['code' => -1, 'message' => $messages[0] -> getMessage()];
		}

		return ['code' => 0, 'user'=>$model];
	}

	public function update($id, $data) {
		if(!$this -> getModelsById($id)) {
			return ['code' => -1, 'message'=>'数据不存在或已删除'];
		}

		if(!$this -> models[$id] -> update($data)) {
			$messages = $this -> models[$id] -> getMessages();
			return ['code' => -1, 'message' => $messages[0] -> getMessage()];
		}

		return ['code' => 0, 'products'=>$this -> models[$id]];
	}

	public function delete($id) {
		if(!$this -> getModelsById($id)) {
			return ['code' => -1, 'message'=>'数据不存在或已删除'];
		}

		if(!$this -> models[$id] -> delete()) {
			$messages = $this -> models[$id] -> getMessages();
			return ['code' => -1, 'message' => $messages[0] -> getMessage()];
		}
		unset($this -> models[$id]);

		return ['code' => 0, 'message'=>'删除失败'];
	}
}
